package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * 检查组的服务提供者实现类
 * @author zhengjd
 * @date 2020/3/30 19:38
 */
@Transactional
@Service
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    CheckGroupDao checkGroupDao;

    /**
     * 检查组的分页查询
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<CheckGroup> checkGroups =checkGroupDao.findPage(queryPageBean);
        PageInfo pageInfo = new PageInfo<>(checkGroups);
        return new PageResult(pageInfo.getTotal(),checkGroups);
    }

    /**
     * 根据检查组id查询检查项的id集合
     */
    @Override
    public List<Integer> findCheckItemIdByCheckGroup(Integer id) {
        return checkGroupDao.findCheckItemIdByCheckGroup(id);
    }

    /**
     *添加检查组
     */
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.add(checkGroup);
        addCheckItemIdAndCheckGroupId(checkGroup.getId(),checkitemIds);
    }

    /**
     *编辑检查组
     */
    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.edit(checkGroup);
        //删除检查组与检查项关联的数据
        checkGroupDao.deleteCheckItemIdByCheckGroupId(checkGroup.getId());
        addCheckItemIdAndCheckGroupId(checkGroup.getId(),checkitemIds);
    }

    /**
     * 循环添加检查项id和检查组id到表t_checkitem_checkgroup
     */
    @Override
    public void addCheckItemIdAndCheckGroupId(Integer checkGroupId, Integer[] checkitemIds) {
        if (checkitemIds != null && checkitemIds.length>0){
            for (Integer checkitemId : checkitemIds) {
                HashMap<String, Integer> map = new HashMap<>();
                map.put("checkGroup_id",checkGroupId);
                map.put("checkItem_id",checkitemId);
                checkGroupDao.addCheckItemIds(map);
            }
        }
    }

    /**
     * 删除检查组
     */
    @Override
    public void delete(Integer id) {
        int checkItemIdCount = checkGroupDao.checkItemsByCheckGroupId(id);
        if (checkItemIdCount>0){
            throw new RuntimeException(MessageConstant.ERROR1_CHECKGROUP_DELETE_FAIL);
        }
        int setMealIdCount = checkGroupDao.setMealsByCheckGroupId(id);
        if (setMealIdCount>0){
            throw new RuntimeException(MessageConstant.ERROR2_CHECKGROUP_DELETE_FAIL);
        }
        checkGroupDao.delete(id);
    }

    /**
     * 查询所有检查组
     */
    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }

    /**
     * 根据id查询检查组
     */
    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupDao.findById(id);
    }
}
